package com.carlinfo.stu_ques.demo.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.imageio.stream.FileImageInputStream;

import com.carlinfo.stu_ques.common.dao.impl.BaseDaoImpl;
import com.carlinfo.stu_ques.common.util.FileUtil;
import com.carlinfo.stu_ques.demo.dao.IHuLuWaDao;
import com.carlinfo.stu_ques.demo.pojo.HuLuWa;

public class HuLuWaDaoImpl    implements IHuLuWaDao 
{
	private static int id;
	private Map<Integer,HuLuWa>  huluwaMap = new TreeMap<Integer,HuLuWa>();
	private FileUtil fileUtil = new FileUtil();
	private File huluwaFile = new File("d:/test/dynasty01.dat");
	public	void idCount()
	{
		id++;
	}
	public int saveOneDao(HuLuWa huluwa)
	{
		this.idCount();
		huluwa.setId(id);
		this.huluwaMap.put(huluwa.getId(), huluwa);
		writeFile();
		return huluwa.getId();
	}

	@Override
	public void updateOneDao(HuLuWa huluwa)
	{
		this.huluwaMap.put(huluwa.getId(), huluwa);
		writeFile();
	}

	@Override
	public void deleteOneDao(HuLuWa huluwa)
	{
		this.huluwaMap.remove(huluwa.getId());
		writeFile();
	}

	@Override
	public List<HuLuWa> selectListDao()
	{	//查询多条信息需要容器
		List<HuLuWa> list = new ArrayList<HuLuWa>();
		//将保存的数据循环出来
		Set<Entry<Integer,HuLuWa>>  entrySet  = this.huluwaMap.entrySet();
		for (Iterator iterator = entrySet.iterator(); iterator.hasNext();)
		{
			Entry<Integer, HuLuWa> entry = (Entry<Integer, HuLuWa>) iterator.next();
			int key = entry.getKey();
			HuLuWa huluwa = entry.getValue();
			//在容器里添加对象
			list.add(huluwa);
		}
		return list;
	}

	@Override
	public HuLuWa selectOneDao(Map<String, Object> condMap)
	{
		if (condMap.get("id")!=null)
		{
			if (condMap.get("id") instanceof Integer)
			{
				Integer id = (Integer) condMap.get("id");
				return huluwaMap.get(id);
			}
		}
		return null;
	}
	public HuLuWaDaoImpl()
	{
		try
		{
			FileInputStream  fis = new FileInputStream(huluwaFile);
			Object obj = this.fileUtil.readObject(fis);
			if (obj!=null)
			{
				List<Object>  list = new ArrayList<Object>();
				list = (List<Object>) obj;
				this.huluwaMap = (Map<Integer, HuLuWa>) list.get(0);
				this.id =  (int) list.get(1);
			}
		} catch (FileNotFoundException e)
		{
//		System.out.println("输入的文件不存在");
//			e.printStackTrace();
		}
	}
	public void writeFile()
	{
		List<Object> list = new ArrayList<Object>();
		list.add(huluwaMap);
		list.add(id);
		try
		{
			if (!huluwaFile.getParentFile().exists())
			{
				huluwaFile.getParentFile().mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(huluwaFile);
			if (!this.fileUtil.writeObject(fos, list))
			{
				System.out.println("我村失败了");
			}
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
